﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;

namespace Einstein.Group.DAL
{
    public class UserInfoDAL
    {

        public IList<UserInfo> SelectAll()
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.UserInfo.ToList<UserInfo>();
            }
        }

        public bool Add(UserInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                entities.UserInfo.AddObject(info);
                return entities.SaveChanges() > 0;
            }
        }
        public bool ExistUserId(UserInfo info, bool IsEdit)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                if (!IsEdit)
                {
                    return entities.UserInfo.Count(a => a.UserID == info.UserID) > 0;
                }
            }
            return false;
        }

        public bool ExistUserName(UserInfo info, bool IsEdit)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                if (IsEdit)
                {
                    return entities.UserInfo.Count(a => a.UserID != info.UserID && a.UserName == info.UserName) > 0;
                }
                else
                {
                    return entities.UserInfo.Count(a => a.UserName == info.UserName) > 0;
                }
            }
        }
        public bool ExistCardID(UserInfo info, bool IsEdit)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                if (IsEdit)
                {
                    return entities.UserInfo.Count(a => a.UserID != info.UserID && a.CardID == info.CardID) > 0;
                }
                else
                {
                    return entities.UserInfo.Count(a => a.CardID == info.CardID) > 0;
                }
            }
        }
        public bool ExistPhysicalID(UserInfo info, bool IsEdit)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                if (IsEdit)
                {
                    return entities.UserInfo.Count(a => a.UserID != info.UserID && a.PhysicalID == info.PhysicalID) > 0;
                }
                else
                {
                    return entities.UserInfo.Count(a => a.PhysicalID == info.PhysicalID) > 0;
                }
            }
        }
        public bool Update(UserInfo info)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {
                UserInfo item = entities.UserInfo.FirstOrDefault(a => a.UserID == info.UserID);
                if (item != null)
                {
                    item.UserName = info.UserName;
                    item.DeptID = info.DeptID;
                    item.CardID = info.CardID;
                    item.PhysicalID = info.PhysicalID;
                    item.UserTypeID = info.UserTypeID;
                    item.Sex = info.Sex;
                    item.PhoneNumber = info.PhoneNumber;
                    item.IdNo = info.IdNo;
                    item.Birthday = info.Birthday;
                    item.Address = info.Address;
                    item.PostCode = info.PostCode;
                    item.OpenCode = info.OpenCode;
                    item.RegDate = info.RegDate;
                    item.ExpireDate = info.ExpireDate = info.ExpireDate;
                }
                return entities.SaveChanges() > 0;
            }
        }
        public bool Delete(UserInfo info)
        {
            using (EinsteinEntities context = new EinsteinEntities())
            {
                var item = context.UserInfo.FirstOrDefault(a => a.UserID == info.UserID);
                if (item != null)
                {
                    context.DeleteObject(item);
                }
                return context.SaveChanges() > 0;
            }
        }

        public UserInfo GetModelByID(UserInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.UserInfo.FirstOrDefault(a => a.UserID == info.UserID);
            }
        }
    }
}
